package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.DepartmentMapper;
import com.example.hospitalsystem.pojo.dto.department.AddDepartmentDto;
import com.example.hospitalsystem.pojo.dto.department.DepartmentPageQueryDto;
import com.example.hospitalsystem.pojo.entity.Department;
import com.example.hospitalsystem.pojo.entity.Doctor;
import com.example.hospitalsystem.pojo.entity.Pharmacy;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
        implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Result<Object> getAllDepartmentInfo(DepartmentPageQueryDto departmentPageQueryDto) {
        Page<Department> departmentPage=new Page<>(departmentPageQueryDto.getPageNum(),departmentPageQueryDto.getPageSize());
        IPage<Department> departmentIPage=baseMapper.selectPage(departmentPage,null);
        return Result.build(departmentIPage,200,"管理员获取所有科室信息成功");

    }

    @Override
    public Result<Object> addDepartnment(AddDepartmentDto addDepartmentDto) {
        Department department=new Department();
        BeanUtils.copyProperties(addDepartmentDto,department);
        int is_success=baseMapper.insert(department);
        if(is_success>0){
            return Result.build(null,200,"添加科室成功");
        }else{
            return Result.build(null,200,"添加科室失败");

        }
    }

    @Override
    public Result<Object> deleteDepartment(int id) {
        LambdaQueryWrapper<Department> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Department::getDepartmentId,id);
        int is_success=baseMapper.delete(lambdaQueryWrapper);
        if(is_success>0){
            return Result.build(null,200,"注销科室成功");
        }else{
            return Result.build(null,200,"注销科室失败");

        }
    }


    /**
     * 返回科室列表
     * @return
     */
    @Override
    public List<Department> getDepartmentList() {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        List<Department> list = departmentMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Department> searchDepartment(String key) {
        List<Department> list = departmentMapper.search(key);
        return list;
    }

    /**
     * 根据id查询科室信息
     * @param departmentId
     * @return
     */
    @Override
    public Result<Object> getDepartmentInfoById(Integer departmentId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getDepartmentId, departmentId);
        Department department = departmentMapper.selectOne(queryWrapper);
        return Result.ok(department);
    }


}
